package dapps.web;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.DateUtils;
import org.springside.modules.web.struts2.CRUDActionSupport;
import org.springside.modules.web.struts2.Struts2Utils;

import dapps.entity.proj.Plan;
import dapps.entity.proj.Projstates;
import dapps.service.ServiceException;
import dapps.service.proj.PlanManager;
import dapps.service.proj.TypeManager;
import dapps.service.user.UserManager;


@SuppressWarnings("serial")
@Results( { 
	@Result(name = CRUDActionSupport.RELOAD, location = "success!input.action?from=${from}", type = "redirect"),
	})
public class SuccessAction extends CRUDActionSupport<Plan> {


	@Autowired
	private PlanManager planManager;	
	@Autowired
	private TypeManager typeManager;
	@Autowired
	private UserManager userManager;
	// 基本属性
	private Plan entity;
	private Long id;
	private List<Projstates> allStates;
	private Integer thisyear;
	private Integer from;
	private List<Plan> plans;
	private Page<Plan> page = new Page<Plan>(6);
	private String[] plannames ={"--","项目申报启动","项目申报结束","项目补报启动","项目补报结束","项目计划下达","进度申报启动","进度申报结束"};
	ServletContext application = Struts2Utils.getApplication();
	// 角色相关属性

	// 基本属性访问函数 //

	public List<Projstates> getAllStates() {
		return allStates;
	}

	public List<Plan> getPlans() {
		return plans;
	}


	public Integer getThisyear() {
		return thisyear;
	}

	public Plan getModel() {
		return entity;
	}
	

	
	@Override
	protected void prepareModel() throws Exception {		
		plans = getList();
	}

	public void setId(Long id) {
		this.id = id;
	}

	// CRUD Action 函数 //
    @SuppressWarnings("deprecation")
	private List<Plan> getList()throws Exception{
    	thisyear = DateUtils.getCurrentDateTime().getYear()+1900;
		Criterion[] criterions = {
				Restrictions.gt("yeartime", thisyear*100)
		};
		Order[] orders={
				Order.desc("yeartime")
		};
		return planManager.FullSearch(null, criterions, null, orders);
    }
    
    private void putApplication(List<Plan> planlist)throws Exception{
		Integer[] planarray = {0,0,0};
		for(Plan p:planlist){				
			switch(p.getStatus()){
			case 1:planarray[0]=p.getYeartime(); break;//申报
			case 3:planarray[1]=p.getYeartime(); break;//补报
			case 6:	if(p.getYeartime()>planarray[2]) planarray[2]=p.getYeartime(); break;//进度申报
			}			
		}
		synchronized(application){
			application.setAttribute("planarray", planarray);
		}
    }

	
	@Override
	public String list() throws Exception {				
		if(application.getAttribute("planarray")==null){
			putApplication(getList());
		}
		if(application.getAttribute("allTypesOrder")==null){			
			synchronized(application){
				application.setAttribute("allTypesOrder", typeManager.getAllTypesChoose());
			}
		}
		if(application.getAttribute("alldutycomp")==null){
			synchronized(application){
				application.setAttribute("alldutycomp", userManager.getUsersOrderByRole("duty"));
			}
		}
		return SUCCESS;
	}

	
	@Override
	public String input() throws Exception {
		
		return INPUT;
	}

	@Override
	public String save() throws Exception {		

		if((SpringSecurityUtils.getCurrentUser().getRoleNames()).indexOf("statesrole")==-1){
			return RELOAD;
		}

		HttpServletRequest request = Struts2Utils.getRequest();
		if (id != null) {
			if(id!=0)	entity = planManager.get(id);
			else {
				entity = new Plan();
				String addyeartime = request.getParameter("addyeartime");
				if(StringUtils.isNotBlank(addyeartime)){
					entity.setYeartime((Integer.parseInt(addyeartime)));
					planManager.save(entity);
					addActionMessage("添加计划成功");
				}else {
					addActionMessage("添加计划失败，请正确选择计划批次");return INPUT;
				}
			}
		} else {
			
			return RELOAD;		
		}
		
		String statusstr = request.getParameter("statusi"+id);
		if(StringUtils.isNotBlank(statusstr)){
			entity.setStatus(Integer.parseInt(statusstr));
			planManager.save(entity);
			putApplication(plans);			
			addActionMessage("保存状态成功");
		}
		

		
		return RELOAD;
	}

	
	@Override
	public String delete() throws Exception {
		try {
			planManager.delete(id);
			addActionMessage("删除计划批次成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}

	public Page<Plan> getPage() {
		return page;
	}

	public void setPage(Page<Plan> page) {
		this.page = page;
	}

	public String[] getPlannames() {
		return plannames;
	}

	public Integer getFrom() {
		return from;
	}

	public void setFrom(Integer from) {
		this.from = from;
	}



	// 其他属性访问函数与Action函数 //




}
